package mysql;

import java.sql.Connection;

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

//TODO:数据库连接及简单ORM框架
public class Conn {
    private Connection conn = null;
    public  Statement statement = null;
    public  PreparedStatement pstate = null;
    private   ResultSet resultSet = null;
    public void getConn() {
        //forName加载驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver" );
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try{
            //getConnection建立连接
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/authorization", "root", "12345678");
            statement = conn.createStatement();
        }catch (SQLException e){
            e.printStackTrace();
        }

    }
    //TODO：用户数据查询
    public ResultSet select(String sql) throws SQLException {

        try {
            pstate = conn.prepareStatement("select * from user where username =\""+sql+"\"");
            resultSet = pstate.executeQuery();
        }catch (SQLException e){
            e.printStackTrace();
            return resultSet;
        }
        return resultSet;

    }
    //TODO：获取未发布的授权码
    public ResultSet selectau(String sql) throws SQLException {
        try {
            pstate = conn.prepareStatement("select * from authorization_code where state = 0 and level = "+sql+" limit 1");
            resultSet = pstate.executeQuery();
        }catch (SQLException e){
            e.printStackTrace();
            return resultSet;
        }
        return resultSet;
    }

    public boolean insert_code(String a_code,int level) throws SQLException{
        try {
            pstate = conn.prepareStatement("INSERT INTO authorization_code (a_code,level,state) VALUES (?,?,0)");
            pstate.setString(1,a_code);
            pstate.setInt(2,level);
            pstate.execute();
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }

    }


    //TODO：改变授权码的发布状态
    public boolean updatestate1(String sql) throws SQLException {
        try {
            pstate = conn.prepareStatement("update authorization_code set state = 1 where a_code = '"+sql+"'");
            pstate.execute();
            return true;
        }catch (SQLException e){
            e.printStackTrace();
            return false;

        }
    }


    //TODO：改变授权码的激活状态
    public boolean updatestate2(String sql) throws SQLException {
        try {
            pstate = conn.prepareStatement("update user_code set state = 1 where a_code = '"+sql+"'");
            pstate.execute();
            return true;
        }catch (SQLException e){
            e.printStackTrace();
            return false;

        }
    }

    //TODO：更新USER_CODE数据表(关联用户与授权码)
    public boolean Insertusercode(String username,String au) throws SQLException {
        try {
            pstate = conn.prepareStatement("insert into user_code(username,a_code,state) values (?,?,0)");
            pstate.setString(1,username);
            pstate.setString(2,au);
            pstate.execute();
            return true;
        }catch (SQLException e){
            e.printStackTrace();
            return false;

        }
    }

    //TODO：判断授权码是否激活0/1/2 未激活 已激活 不存在
    public int checkstate(String au) throws SQLException {
        try {
            pstate = conn.prepareStatement("select * from user_code where a_code = ? limit 1");
            pstate.setString(1,au);
            resultSet = pstate.executeQuery();
            if (resultSet.next()){
                return  resultSet.getInt("state");
            }

        }catch (SQLException e){
            e.printStackTrace();

        }
        return -1;
    }
    public void close() throws SQLException {
        conn.close();
    }
}
